package com.zhanghe.study.springcloud2.controller;

import com.zhanghe.study.model.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zh
 * @date 2021/8/20 15:06
 */
@RestController
public class TestDiscoveryController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/discovery/info")
    public void get() {
        // 获取所有服务
        List<String> list = discoveryClient.getServices();
        System.out.println("*****"+list+"*****");

        // 找到某个微服务的所有实例
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("micro_service_dept_provider");
        for(ServiceInstance serviceInstance : serviceInstances){
            System.out.println(serviceInstance.getServiceId() + "----"+serviceInstance.getHost()
            +"-----"+serviceInstance.getPort()+"-----"+serviceInstance.getUri());
        }

    }
}
